package net.babypai;

import net.babypai.utils.LocalMediaPlayer;
import net.babypai.views.CircleProgress;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;



public class MusicPlayerLocal extends Activity implements OnClickListener {
	private Button close;
	private String path;
	private TextView playTime;
	private ImageView musicplayer_img;
	private RelativeLayout dialog_layout;
	private LocalMediaPlayer audioLocal;
	private CircleProgress mCircleProgressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music_player);
		playTime = (TextView) findViewById(R.id.play_time);
		dialog_layout = (RelativeLayout) findViewById(R.id.dialog_layout);
		musicplayer_img = (ImageView) findViewById(R.id.musicplayer_img);
		path = getIntent().getStringExtra("path");
		close = (Button) findViewById(R.id.close);
		close.setOnClickListener(this);
		dialog_layout.setOnClickListener(this);
		startLocalAudio(path);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event){
		if (audioLocal != null) {
			audioLocal.getMediaPlayer().pause();
			audioLocal.getMediaPlayer().stop();
			audioLocal = null;
		}
		MusicPlayerLocal.this.finish();
		return true;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.close:
			if (audioLocal != null) {
				audioLocal.getMediaPlayer().pause();
				audioLocal.getMediaPlayer().stop();
				audioLocal = null;
			}
			MusicPlayerLocal.this.finish();
			break;
		case R.id.dialog_layout:
			if(audioLocal.getMediaPlayer() != null){
				if (audioLocal.getMediaPlayer().isPlaying()) {
					audioLocal.getMediaPlayer().pause();
				} else {
					audioLocal.getMediaPlayer().start();
					audioLocal.startPlayProgressUpdater();
				}
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (audioLocal.getMediaPlayer() != null) {
				audioLocal.getMediaPlayer().pause();
				audioLocal.getMediaPlayer().stop();
				audioLocal = null;
			}
			MusicPlayerLocal.this.finish();
		}
		return false;
	}
	
	
	private void startLocalAudio(String path) {
		System.out.println("path---->" + path);
		AnimationDrawable anim = null;
		Object ob = musicplayer_img.getBackground();
		anim = (AnimationDrawable) ob;
		anim.stop();
		anim.start();
		mCircleProgressBar = (CircleProgress) findViewById(R.id.roundBar3);
		audioLocal = new LocalMediaPlayer(this, mCircleProgressBar, playTime);
		stopThis();
		audioLocal.startMediaPlayer(path);

	}
	
	private final Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
		}
	};
	
	private void stopThis(){
		float progress = mCircleProgressBar.getMainProgress();
		System.out.println("progress--music-->" + progress);
		if(progress > 99){
			if (audioLocal.getMediaPlayer() != null) {
				audioLocal.getMediaPlayer().pause();
				audioLocal.getMediaPlayer().stop();
				audioLocal = null;
			}
			MusicPlayerLocal.this.finish();
		}
		if (audioLocal != null) {
			Runnable notification = new Runnable() {
		        public void run() {
		        	stopThis();
				}
		    };
		    handler.postDelayed(notification,1000);
    	}
	}
}